/**
 *  随时 更新canvas （画布） 大小
 * */
import React, { useCallback, useEffect, useState } from 'react';
import { Engine, Layout } from '../DesignerModels';

interface ILayoutRef {
  width?: number;
  height?: number;
}

export const useCanvasLayout = (
  id: string,
  engine: Engine | null | undefined,
  initSize: any,
) => {
  const [size, setSize] = useState<Record<string, any>>(initSize);

  const setCanvaSize = useCallback(
    (size) => {
      setSize(size);
    },
    [size],
  );

  useEffect(() => {
    let layout = new Layout({ id });

    const resize = (width: any, height: any) => {
      engine?.onResize(width, height);

      setCanvaSize({
        width,
        height,
      });
    };

    layout.onResize(id, resize);
  }, [id]);

  return {
    size,
  };
};
